using I2.Loc;
using UnityEngine;

public class ContextTutorLostDistrict : MonoBehaviour
{
	[SerializeField]
	private string term;

	[SerializeField]
	private float time = 10f;

	[SerializeField]
	private string nameChildForPointer = "Stats";

	[SerializeField]
	private Vector3 pointerLocalPosition;

	[SerializeField]
	private GameObject panelPrefab;

	[SerializeField]
	private GameObject pointerPrefab;

	[SerializeField]
	private MapDistrict[] districts;

	private InGameTutorBlockPanel panel;

	private GameObject pointer;

	private bool init;

	private void OnEnable()
	{
		if (districts == null || districts.Length == 0)
		{
			districts = GetComponentsInChildren<MapDistrict>();
		}
		if (!init)
		{
			for (int i = 0; i < districts.Length; i++)
			{
				districts[i].DistrictLost += OnDistrictLost;
			}
			init = true;
		}
	}

	private void OnDestroy()
	{
		for (int i = 0; i < districts.Length; i++)
		{
			districts[i].DistrictLost -= OnDistrictLost;
		}
	}

	private void OnDistrictLost()
	{
		if (panel == null)
		{
			panel = Object.Instantiate(panelPrefab).GetComponent<InGameTutorBlockPanel>();
		}
		panel.transform.parent = base.transform;
		panel.transform.localScale = Vector3.one;
		panel.EnableBlock(false);
		panel.SetText(ScriptLocalization.Get(term), true);
		for (int i = 0; i < districts.Length; i++)
		{
			if (districts[i].losted)
			{
				if (pointer == null)
				{
					pointer = Object.Instantiate(pointerPrefab);
				}
				pointer.transform.parent = districts[i].transform;
				pointer.transform.localPosition = districts[i].transform.FindChild(nameChildForPointer).transform.localPosition + pointerLocalPosition;
				pointer.transform.localScale = Vector3.one;
				break;
			}
		}
		for (int j = 0; j < districts.Length; j++)
		{
			districts[j].DistrictLost -= OnDistrictLost;
		}
		Invoke("DisableText", time);
	}

	public void Update()
	{
		Touch[] touches = Input.touches;
		foreach (Touch touch in touches)
		{
			if (touch.phase == TouchPhase.Began)
			{
				DisableText();
			}
		}
		if (Input.GetMouseButtonDown(0))
		{
			DisableText();
		}
	}

	private void DisableText()
	{
		if (!(panel == null))
		{
			BCWStorage.Set("ContextTutorLostDistrict", false);
			Object.Destroy(pointer);
			Object.Destroy(panel.gameObject);
			base.enabled = false;
		}
	}
}
